package org.sucri.servlet;

import org.sucri.dao.NewsEntity;
import org.sucri.floxs.html.Entity2Html_Factory;
import org.sucri.floxs.html.TemplateDictionary;
import org.sucri.floxs.html.TemplatePage;
import org.sucri.floxs.html.TestTemplateDictionary;
import org.sucri.floxs.servlet.FrontServlet;
import org.sucri.floxs.servlet.Servlet_Util;
import org.sucri.servlet.handler.LoginHandler;
import org.sucri.servlet.handler.NewsEntryHandler;
import org.sucri.servlet.handler.RegisterHandler;
import org.sucri.servlet.handler.UserInfoHandler;
import org.sucri.servlet.controller.dwrAdmin;
import org.apache.log4j.PropertyConfigurator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Wen Yu
 * Date: Jul 5, 2007
 * Time: 9:00:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class main extends FrontServlet
{
  @Override
  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);

    // setup the log4j stuff
    PropertyConfigurator.configure("C:\\Apps\\projects\\floxs\\FigureEight\\log4j.properties");

    // deposite the handlers
    new LoginHandler(    _Handlers);
    new UserInfoHandler( _Handlers);
    new RegisterHandler( _Handlers);
    new NewsEntryHandler(_Handlers);

    Dao_Util.setEntityManagerFactory(emf);
    dwrAdmin admin = new dwrAdmin(emf);
/*
    try
    {
      Class.forName("org.sucri.dao.UsersEntity");
      Class.forName("org.sucri.dao.PetsEntity");
      Class.forName("org.sucri.dao.ItemsEntity");      
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
    }
*/
  }

  @Override
  protected boolean process(HttpServletRequest req,
                            HttpServletResponse res, boolean isGet)
        throws ServletException, IOException
  {
    if (super.process(req, res, isGet)) return true;

    TemplateDictionary dictionary = new TestTemplateDictionary();
    TemplatePage pages = new TemplatePage("basic", mHtmlTemplatePath, null);
    Integer page_id = Servlet_Util.getPageID(req);

    if (page_id != null && page_id == 1)
    {
      //res.getOutputStream().println(pages.generate("news", dictionary));
      res.getOutputStream().println(pages.generate("sucri_admin", dictionary));
    }
    /*else
    {
      // prepare the news entries
      List<NewsEntity> news = Dao_Util.getNewsEntities(5);
      dictionary.setAttribute("news_clip", Entity2Html_Factory.toNewsClip(news, null).toString());
      res.getOutputStream().println(pages.generate("index", dictionary));      
    } */

    return true;
  }
}
